#pragma once
#include<iostream>
#include"./Server/explain.h"
#include"./Socket/tcplisten.h"
#include"./Socket/tcpcommunication.h"
#include"./Socket/epollloop.h"
#include"sqldb.h"
#include"shopinfosql.h"
#include<QDebug>
#include<QString>
using namespace std;

void readCallBack(char* arry,int len,void *arg,TcpCommunication *tcpCon)
{
    auto explain=Explain::getInstance();
    if(len>0)
    {
        QByteArray arryv(arry);
        explain->dispatchServer(arryv,tcpCon);
    }
    else if(len==0)
    {
        //cout<<"断开链接"<<__FUNCTION__<<__LINE__<<endl;
        ClientArg *arg=(ClientArg *)tcpCon->getReadArg();
        arg->clientDisconnect(tcpCon,explain->getHall());
    }
}

void writeCallBack(void *arg,TcpCommunication *tcpCon)
{
    tcpCon->changeStatus(TcpCommunication::Status::Read);
}

void acceptCallBack(TcpCommunication *tcpCon)
{
    tcpCon->setReadCallBack(readCallBack,nullptr);
    tcpCon->setWriteCallBack(writeCallBack,nullptr);
}

int main()
{
    SqlDb::getInstance();
    EpollLoop loop(7777);
    loop.setReadcb  (readCallBack);
    loop.setWritecb (writeCallBack);
    loop.setAcceptcb(acceptCallBack);
    loop.setCanUse();

    loop.initHeartTime(100);
    loop.startAlarm();
    loop.loop();
    return 1;
}

int main123123123()
{
    SqlDb::getInstance();
//    //增加用户表
//    ShopActSql mylit("./info.db");

    string createTable="create table user(user_name varchar(20) primary key,"
                             " cipher varchar(20))";


    if(! SqlDb::getInstance()->doSql(createTable))
    {
        cout<<"create error"<<endl;
    }


    string insertValue="insert into user(user_name, cipher)"
                       " values('12345', '123')";
    if(!SqlDb::getInstance()->doSql(insertValue))
    {
        cout<<"insert error"<<endl;
    }
    QString sql=QString("select * from user where user_name='%1'and cipher='%2'").
                                                            arg("123","123");
    string showtab="select  *from user where user_name='12345' and cipher='123'"
               ;



    if(!SqlDb::getInstance()->selectDB(showtab))
    {
        cout<<"show error"<<endl;
    }

    return 1;
}
